package problema018;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;

public class SumaPiramide {
	static int[][] piramide;
	static int[][] sol;
	
	public static void main(String[] args){
		//sumaPiramide15();
		System.out.println(sumaPiramide15());
	}
	
	public static int sumaPiramide15(){
		leer();
		for(int i = 0; i < 15; i++) sol[14][i] = piramide[14][i];
		resolver(0,0);
		return sol[0][0];
	}
	
	public static int resolver(int x, int y){
		if(sol[x][y] != 0){
			return sol[x][y];
		}else{
			sol[x][y] = maximo(resolver(x+1,y),resolver(x+1,y+1)) + piramide[x][y];
			return sol[x][y];
		}
	}

	public static int maximo(int x, int y){
		if(x < y) return y;
		return x;
	}
	
	public static void leer(){
		piramide = new int[15][15];
		sol = new int[15][15];
		try{
			BufferedReader reader = new BufferedReader( new FileReader("piramideEj18.txt") );
			String linea;
			int fila = 0;
			while ((linea = reader.readLine()) != null){
				StringTokenizer st = new StringTokenizer(linea, " " );
				int col = 0;
				while (st.hasMoreTokens()){
					piramide[fila][col] = Integer.parseInt(st.nextToken().trim());
					col++;
				}
				fila++;
			}
			reader.close();
			}catch (Exception e){
				System.err.println("Error: " + e.getMessage());
			}
	}
}
